package cn.sciento.farm.devicemanager.gateway.downlink.service.impl;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttCallBackFuture;
import cn.sciento.farm.devicemanager.gateway.model.uplink.ControlData;
import cn.sciento.farm.devicemanager.gateway.model.uplink.DeviceSystemPacket;
import cn.sciento.farm.devicemanager.gateway.uplink.service.sensor.AbstractMessageDisposeService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

/**
 * @author wumu
 * @version V1.0
 * @date 2/27/18
 * @time 4:25 PM
 */
@Order(5)
@Service(value = "sendDataDisposeService")
public class DataDisposeService extends AbstractMessageDisposeService {

    Logger logger = LoggerFactory.getLogger(DataDisposeService.class);


    /**
     * 设备的开关状态
     */
    @Override
    public void changeSwitch(ControlData switchStatus) {
        MqttCallBackFuture mqttCallBackFuture = MqttCallBackFuture.futurePool.get(
                String.format(
                        MqttConstant.SEND_BLOCK_ID,
                        switchStatus.getEui(),
                        switchStatus.getNode(),
                        switchStatus.getCircuit()
                )
        );
        if (mqttCallBackFuture != null) {
            mqttCallBackFuture.setResponse(String.valueOf(switchStatus.getEn()));
        }
    }


    @Override
    public void changeSystem(DeviceSystemPacket systemPacket) {
        MqttCallBackFuture mqttCallBackFuture = MqttCallBackFuture.futurePool.get(
                String.format(
                        MqttConstant.SCI_CONFIG_BLOCK_ID,
                        systemPacket.getEui()
                )
        );
        if (mqttCallBackFuture != null) {
            mqttCallBackFuture.setResponse(JSON.toJSONString(systemPacket));
        }
    }
}
